package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.ExamRecodingDao;
import com.chunfeng.mypen.entity.ExamRecoding;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.ExamRecodingService;
import com.chunfeng.mypen.vo.ExamRecodingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 考试历史记录方法实现类
 * @author: 小雷
 * @createTime: 2023-11-14 00:20:39
 */
@Service
public class ExamRecodingServiceImpl extends ServiceImpl<ExamRecodingDao, ExamRecoding> implements ExamRecodingService {

    @Autowired
    private ExamRecodingDao examRecodingDao;

    /**
     * 查询所有考试记录
     *
     * @param examRecoding 条件
     * @return JSON
     */
    @Override
    public JsonRequest<List<ExamRecodingVo>> lookExamRecoding(ExamRecoding examRecoding) {
        List<ExamRecodingVo> examRecordings = examRecodingDao.lookExamRecoding(examRecoding);
        if (examRecordings.isEmpty()) {
            return JsonRequest.error(RequestException.SELECT_ERROR);
        }
        return JsonRequest.success(examRecordings);
    }

    /**
     * 新增考试记录信息
     *
     * @param examRecoding 考试记录实体类
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> addExamRecoding(ExamRecoding examRecoding) {
        int insert = this.baseMapper.insert(examRecoding);
        if (insert < 1) {
            JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(insert);
    }

    /**
     * 修改试卷记录信息
     *
     * @param examRecoding 需要修改的实体类
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateExamRecoding(ExamRecoding examRecoding) {
        int update = this.baseMapper.updateById(examRecoding);
        if (update < 1) {
            JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(update);
    }

    /**
     * 删除试卷记录信息
     *
     * @param ids 需要删除的试卷记录id
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteExamRecoding(List<String> ids) {
        //判断删除的ids是否有值
        if (ids.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //判断删除的ids是否存在
        Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<ExamRecoding>()
                .in(ExamRecoding::getId, ids));
        if (count != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //删除ids
        int delete = this.baseMapper.deleteBatchIds(ids);
        if (delete != count) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(delete);
    }
}
